package org.androidpn.server.dao.impl;

import java.util.List;

import javax.persistence.TypedQuery;

import org.androidpn.server.dao.FriendDao;
import org.androidpn.server.entity.Friend;
import org.springframework.stereotype.Repository;

/**
 * Dao Friend
 * 
 * @author 蔡琪
 * 
 */
@Repository("friendDao")
public class FriendDaoImpl extends BaseDaoImpl<Friend, Long> implements
		FriendDao {

	public Friend findFriend(Long senderId, Long receiverId) {
		String jpql = "from Friend fr  where fr.role.id=:senderId and fr.friendGroup.role.id=:receiverId";
		TypedQuery<Friend> query = entityManager
				.createQuery(jpql, Friend.class)
				.setParameter("senderId", senderId)
				.setParameter("receiverId", receiverId);
		List<Friend> friends = query.getResultList();
		return friends.isEmpty() ? null : friends.get(0);
	}

}
